/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.delivery.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.Date;

import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.javaeye.common.dto.DTO;
import com.javaeye.delivery.dto.DeliveryOrder;
import com.javaeye.common.web.PageInfo;
import com.javaeye.delivery.web.OrderCondition;

public class DeliveryOrderDAOHibernate extends HibernateDaoSupport implements DeliveryOrderDAO {
	
	@Override
	public void save(DeliveryOrder order) {
		getHibernateTemplate().saveOrUpdate(order);
	}
	
	public DeliveryOrder getDeliveryOrder(String orderId) {
		DeliveryOrder dOrder = (DeliveryOrder)getHibernateTemplate().get(DeliveryOrder.class, orderId);
		//由于使用延迟加载，这里需要强制取出关联集合
		Hibernate.initialize(dOrder.getOrderDetails());
		return dOrder;
	}
	
	@SuppressWarnings("unchecked")
	public List getPageData(final DTO contidions, final PageInfo pageInfo) {
		HibernateCallback action = new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				
				int pageIndex = pageInfo.getPageIndex();
				int pageSize = pageInfo.getPageSize();
				OrderCondition contidion = (OrderCondition)contidions;
				Criteria crit = session.createCriteria(DeliveryOrder.class);
				
				String orderNo = contidion.getOrderNo().trim();
				if (!"".equals(orderNo)) {
					crit.add(Restrictions.like("orderNo", "%" + orderNo + "%"));
				}
				
				String customName = contidion.getCustomName().trim();
				if (!"".equals(customName)) {
					crit.add(Restrictions.like("customName", "%" + customName + "%"));
				}
				
				String logisticsName = contidion.getLogisticsName().trim();
				if (!"".equals(logisticsName)) {
					crit.add(Restrictions.like("logisticsName", "%" + logisticsName + "%"));
				}
				
				Date beginDate = contidion.getBeginDate();
				Date endDate = contidion.getEndDate();
				if (beginDate != null && endDate != null) {
					crit.add(Restrictions.between("deliveryDate", beginDate, endDate));
				}
				
				//出货单号不为空
				crit.add(Restrictions.isNotNull("orderNo"));
				
				crit.setProjection(Projections.rowCount());
				Integer rowNumber = (Integer)crit.uniqueResult();
				pageInfo.setCount(rowNumber);
				
				crit.setProjection(null);//取得了总数后,为了能够再次取得对象列表
				crit.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);//当hibernate采用关联的时候不使用这个返回的是个对象数组，加上这个返回的是当前列表的对象。
				crit.setFirstResult(pageIndex * pageSize);
				crit.setMaxResults(pageSize);
				return crit.list();
			}
		};
		return getHibernateTemplate().executeFind(action);
	}

}
